using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using FileFolderNavigator;
using System.Text.RegularExpressions;

namespace FolderBackupInterface
{
    public partial class Action : UserControl
    {
        public string TypeText { get { return TypeTextBox.Text; } }
        public string DaysText { get { return DayTextBox.Text; } }
        public string LastText { get { return LastTextBox.Text; } }
        public string TimeText { get { return TimePicker.Text; } }
        private ActionElement _act;

        public Action()
        {
            InitializeComponent();
            TypeTextBox.Leave += new EventHandler(TypeTextBox_TextChanged);
            //TimeTextBox.TextChanged += new EventHandler(TimeTextBox_TextChanged);
            //TimePicker.TextChanged += new EventHandler(TimeTextBox_TextChanged);
            DayTextBox.Leave += new EventHandler(DayTextBox_Leave);
            LastTextBox.TextChanged += new EventHandler(LastTextBox_TextChanged);
            //DayTextBox.TextChanged += new EventHandler(DayTextBox_TextChanged);
        }

        void DayTextBox_Leave(object sender, EventArgs e)
        {
            Regex r = new Regex("([1-9])+");
            TextBox s = (TextBox)sender;
            if (r.IsMatch(s.Text))
            {
                if (_act != null)
                {
                    _act.Days = ((TextBox)sender).Text;
                }
            }
            else
            {
                (sender as TextBox).Clear();
                (sender as TextBox).Focus();
            }            
        }

        void DayTextBox_TextChanged(object sender, EventArgs e)
        {
            Regex r = new Regex("[0-9]*");
            TextBox s = (TextBox)sender;
            if (r.IsMatch(s.Text))
            {
                if (_act != null)
                {
                    _act.Days = ((TextBox)sender).Text;
                }
            }
            else
            {
                
            }
        }

        void LastTextBox_TextChanged(object sender, EventArgs e)
        {
            if (_act != null)
            {
                _act.Last = ((TextBox)sender).Text; 
            }
        }

        void TimeTextBox_TextChanged(object sender, EventArgs e)
        {
            if (_act != null)
            {
                _act.Time = ((DateTimePicker)sender).Text; 
            }
        }

        void TypeTextBox_TextChanged(object sender, EventArgs e)
        {
            Regex r = new Regex("^(sync|update)$");
            if (r.IsMatch((sender as TextBox).Text))
            {
                if (_act != null)
                {
                    _act.Type = ((TextBox)sender).Text;
                }
            }
            else
            {
                (sender as TextBox).Clear();
                (sender as TextBox).Focus();
            }
        }

        public void SetValues(ActionElement act) 
        {
            _act = act;

            TypeTextBox.Text = act.Type;
            try
            {
                TimePicker.Text = act.Time;
            }
            catch
            {
                act.Time = DateTime.Now.TimeOfDay.ToString();
                TimePicker.Text = act.Time;
            }
            LastTextBox.Text = act.Last;
            DayTextBox.Text = act.Days;
        }

        private void Action_Load(object sender, EventArgs e)
        {

        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            if (_act != null)
            {
                _act.Time = ((DateTimePicker)sender).Text;
            }
        }
    }
}
